package com.example.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import com.example.common.CustomerException;
import com.example.common.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/files")
public class FileController {
    @GetMapping("/download/{fileName}")
    public void download(@PathVariable String fileName,HttpServletResponse response) throws IOException {
        System.out.printf("ddd", System.getProperty("user.dir"));
        String filePath=System.getProperty("user.dir")+"/spring/demo/src/main/java/com/example/file/";
        String realPath = filePath + fileName;
        if(!FileUtil.exist(realPath)){
            throw new CustomerException(500,"文件不存在");
        }
        byte[] bytes = FileUtil.readBytes(realPath);
//        response.setContentType("application/octet-stream");
//        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        ServletOutputStream os = response.getOutputStream();
        os.write(bytes);
        os.flush();
        os.close();
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException {
        String filePath=System.getProperty("user.dir")+"/spring/demo/src/main/java/com/example/file/";
        if(!FileUtil.isDirectory(filePath)){
            FileUtil.mkdir(filePath);
        }
        byte[] bytes = file.getBytes();
        String filename= System.currentTimeMillis()+"-"+file.getOriginalFilename();
//        String encodeFilename = URLEncoder.encode(filename, "UTF-8");
        FileUtil.writeBytes(bytes, filePath + filename);
        String url = "http://localhost:8080/files/download/" + filename;
        return Result.success(url);
    }

    @PostMapping("/notice/media/upload")
    public Map<String,Object> noticeUpload(MultipartFile file) throws IOException {
        String filePath=System.getProperty("user.dir")+"/spring/demo/src/main/java/com/example/file/";
        if(!FileUtil.isDirectory(filePath)){
            FileUtil.mkdir(filePath);
        }
        String fileName=System.currentTimeMillis()+"-"+file.getOriginalFilename();
        try{
            FileUtil.writeBytes(file.getBytes(), filePath + fileName);
            Thread.sleep(1L);
        }catch (Exception e){
            System.out.println("文件上传失败");
        }

        Map<String,Object> map = new HashMap<>();
        map.put("errno",0);
        map.put("data", CollUtil.newArrayList(Dict.create().set("url","http://localhost:8080/files/download/"+fileName)));
        return map;
    }
}
